from random import randint
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.expected_conditions import *
from selenium.webdriver.common.by import By
from pages.login_page import LoginPage
from pages.internal_page import InternalPage
from pages.film_view_page import FilmViewPage
from pages.film_add_page import FilmAddPage
from pages.film_list_page import FilmListPage


class Application(object):

    def __init__(self, driver, base_url):
        self.driver = driver
        self.wait = WebDriverWait(driver, 3)
        self.base_url = base_url
        self.login_page = LoginPage(driver, base_url)
        self.internal_page = InternalPage(driver, base_url)
        self.film_view_page = FilmViewPage(driver, base_url)
        self.film_add_page = FilmAddPage(driver, base_url)
        self.film_list_page = FilmListPage(driver, base_url)

    def login(self, user):
        self.driver.get(self.base_url)

        auth = self.login_page.login_fields
        auth[0].send_keys(user.username)
        auth[1].click()
        auth[1].send_keys(user.password)

        self.login_page.submit_button.click()

    def logout(self):
        self.internal_page.logout_link.click()
        self.driver.switch_to_alert().accept()

    def select_random_movie(self):
        self.driver.implicitly_wait(1)

        list = self.film_list_page.movie_boxes
        films_num = len(list)
        index = randint(0,films_num-1)
        list[index].click()

        return films_num

    def delete_this_movie(self):
        self.film_view_page.delete_button.click()
        self.driver.switch_to_alert().accept()

    def assert_film_was_deleted(self, films_num):
        list = self.film_list_page.movie_boxes
        assert len(list) == films_num-1, "Wrong films number"

    def add_movie(self, data):
        # Click Add movie button
        self.film_list_page.add_button.click()
        self.driver.implicitly_wait(1)
        # Add movie info for each field
        for field_name,value in data.iteritems():
            if value != "":
                field = self.film_add_page.field(field_name)
                field.click()
                field.send_keys(value)
        # Click submit
        self.film_add_page.submit.click()

    def assert_film_was_added(self, film):
        text = self.film_view_page.film_info.text
        assert text == film.name+' ('+film.year+')', "Wrong film name"

    def assert_error_message(self):
        error_msg = self.film_add_page.err_msg
        assert error_msg.is_displayed(), "Error message is not displayed"

    def search_movie(self, name):
        search_field = self.film_list_page.search_field
        search_field.click()
        search_field.clear()
        search_field.send_keys(name)
        search_field.send_keys(Keys.RETURN)

    def assert_search_results(self, results_are_present):
        list = self.film_list_page.movie_boxes
        if results_are_present:
            assert len(list) > 0, "No results found"
        else:
            assert len(list) == 0, "Some results found"

    def wait_for_status_message(self, check_message_text=False):
        if check_message_text:
            self.film_list_page.no_search_results_and_check_message
            self.wait.until(text_to_be_present_in_element((By.CLASS_NAME, "content"), "No movies where found."))
        else:
            self.film_list_page.no_search_results
            self.wait.until(presence_of_element_located((By.CLASS_NAME, "content")))

    def wait_for_results_to_appear(self):
        self.film_list_page.search_results
